const deepseek = require('./LLM/deepseek');
const prompts = require('./LLM/prompts');

class HttpRequest2ClientCode {
    /**
     * 生成HTTP请求代码
     * @param {object} requestDesc HTTP请求描述
     * @param {string} techStack 技术栈
     * @returns {Promise<object>} 生成的代码
     */
    async generateRequestCode(requestDesc, techStack = 'axios') {
        techStack = techStack.toLowerCase();

        try {
            const response = await deepseek.call(
                prompts.HttpRequest2ClientCode(requestDesc, techStack),
                { temperature: 0.3 }
            );

            return this._extractCode(response);
        } catch (error) {
            console.error('生成请求代码失败:', error);
            throw new Error('生成请求代码失败: ' + error.message);
        }
    }

    _extractCode(content) {
        // 通用代码块匹配，不限定语言
        const match = content.match(/```[\s\S]*?\n([\s\S]*?)```/);
        return match ? match[1].trim() : content;
    }
}

module.exports = new HttpRequest2ClientCode();